package com.shuwen.gcdj.controller.basics;

import com.shuwen.gcdj.bean.DeleteReq;
import com.shuwen.gcdj.bean.SelectReq;
import com.shuwen.gcdj.bean.basicsEquitment.request.*;
import com.shuwen.gcdj.bean.basicsEquitment.response.FileDictResp;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.common.api.CommonPage;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.service.basics.EquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.FileNotFoundException;
import java.util.List;


@Api(tags = "设计资料")
@Slf4j
@RestController
@RequestMapping
public class FileController extends AuthController {

    @Autowired
    private EquipmentService equipmentService;

    @ApiOperation(value = "设计资料-上传文件")
    @PostMapping(value = "/designinformation/upload")
    @ResponseBody
    @JWTAuth
    public CommonResult<String> uploadFile(MultipartFile file) {
        return equipmentService.uploadFile(file);
    }


    @ApiOperation(value = "报修-上传文件")
    @PostMapping(value = "/designinformation/uploadFlow")
    @ResponseBody
    @JWTAuth
    public CommonResult<String> uploadFileFlow(MultipartFile file) {
        return equipmentService.uploadFileFlow(file);
    }

    @ApiOperation(value = "设计资料-上传文件-绑定父级")
    @PostMapping(value = "/designinformation/addFile")
    @ResponseBody
    @JWTAuth
    public CommonResult<String> addFile(@RequestBody @Valid FolderBangReq req) {
        return equipmentService.addFile(req);
    }


    @ApiOperation(value = "设计资料-新增文件夹")
    @PostMapping(value = "/designinformation/addfile")
    @ResponseBody
    @JWTAuth
    public CommonResult<String> addFile(@RequestBody @Valid FolderAddReq req) {
        return equipmentService.addFile(req);
    }

    @ApiOperation(value = "设计资料-下载文件")
    @GetMapping(value = "/designinformation/downfile")
    @ResponseBody
    @JWTAuth
    public void downloadZip(@RequestParam String id, HttpServletResponse response) {
        equipmentService.fileDetails(id, response);
    }

    @ApiOperation(value = "设计资料-重命名")
    @PostMapping(value = "/designinformation/editresouce")
    @ResponseBody
    @JWTAuth
    public CommonResult<String> restFile(@RequestBody @Valid FolderUpdate req) {
        return equipmentService.restFile(req);
    }

    @ApiOperation(value = "设计资料-移动")
    @PostMapping(value = "/designinformation/getdesigninformationfoldertree")
    @ResponseBody
    @JWTAuth
    public CommonResult<String> moveFile(@RequestBody @Valid MoveFileReq req) {
        return equipmentService.moveFile(req);
    }

    @ApiOperation(value = "设计资料-删除")
    @PostMapping(value = "/designinformation/batchdeleteresouce")
    @ResponseBody
    @JWTAuth
    public CommonResult<String> deleteFile(@RequestBody @Valid DeleteReq req) {
        return equipmentService.deleteFile(req.getIds());
    }

    @ApiOperation(value = "设计资料-预览")
    @GetMapping(value = "/designinformation/preview")
    @ResponseBody
    @JWTAuth
    public void filePreview(@RequestBody @Valid SelectReq req, HttpServletResponse response) {
        equipmentService.filePreview(req.getId(), response);
    }


    @ApiOperation(value = "设计资料-文件夹树结构")
    @PostMapping(value = "/designinformation/getdesigninformationpage")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<FileDictResp>> fileChilds(@RequestBody FolderIdReq req) {
        return equipmentService.fileChilds(req);
    }


    @ApiOperation(value = "设计资料-下载文件夹")
    @GetMapping(value = "/designinformation/downfolder")
    @ResponseBody
    @JWTAuth
    public void fileDetails(@RequestParam String id, HttpServletResponse response) throws FileNotFoundException {
        equipmentService.fileDetails(id, response);
    }

    @ApiOperation(value = "获取设计资料")
    @PostMapping(value = "/equipmentmanagement/getrelationresourcelist")
    @ResponseBody
    @JWTAuth
    public CommonResult<CommonPage<FileDictResp>> getFileDetails(@RequestBody FolderListReq req) {
        return equipmentService.getFileDetails(req);
    }

    @ApiOperation(value = "获取所有文件夹")
    @GetMapping(value = "/equipmentmanagement/getFiles")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<FileDictResp>> getFiles() {
        return equipmentService.getFiles();
    }


    @ApiOperation(value = "设计资料-查询文件")
    @PostMapping(value = "/designinformation/selectFolder")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<Object>> selectFolder(@RequestBody @Valid SelectDetailReq req) {
        return equipmentService.selectFile(req);
    }


}
